#! /bin/sh
#

NAME=julep
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/bin/julep-server.py
PIDFILE=/var/run/julep/julep.pid
USER=www-data

test -x $DAEMON || exit 0

set -e

start_server() {
	start-stop-daemon --start --chuid $USER --exec $DAEMON >/dev/null 2>&1 -- -p $PIDFILE $IPADDR $PORT
}

stop_server() {
	start-stop-daemon --stop --retry 2 --signal INT --pidfile $PIDFILE 
}

graceful_restart() {
	/bin/kill -sigusr1 `cat $PIDFILE`
}

case "$1" in
  start)
	echo -n "Starting $DESC: "
	start_server
	echo "$NAME."
	;;
  stop)
	echo -n "Stopping $DESC: "
	stop_server || true
	echo "$NAME."
	;;
  graceful)
	echo -n "Gracefully Restarting $DESC: "
    graceful_restart
	echo "$NAME."
    ;;
  restart)
	echo -n "Stopping $DESC: "
  	stop_server || true
	echo "Stopped"
	echo -n "Starting $DESC: "
	start_server
	echo "Started"
	;;
  *)
	N=/etc/init.d/$NAME
	echo "Usage: $N {start|stop|restart|graceful}" >&2
	exit 1
	;;
esac

exit 0

